using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using Core.SimpleTemp.Application.ServiceApp.SysApp.DataDicApp;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// CBS_DayCheckItem
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_DayCheckItemController : CoreApiController
    {
        private readonly CBS_DayCheckItemService _service;
        private readonly SysDataDicService _servicedds;
        private readonly CBS_TotalCheckItemService _serviceItem;
        /// <summary>
        /// CBS_DayCheckItem
        ///</summary>
        public CBS_DayCheckItemController(CBS_DayCheckItemService service, SysDataDicService servicedds, CBS_TotalCheckItemService serviceItem)
        {
            _service = service;
            _servicedds = servicedds;
            _serviceItem = serviceItem;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_DayCheckItemController) + "_" + nameof(CBS_DayCheckItemController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<CBS_DayCheckItemDto>
                pagingQueryModel)
        {
            IPageModel<CBS_DayCheckItemDto>
                result;

            
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        [HttpPost("CBSsglbGetList")]
        [PermissionFilter(nameof(CBS_DayCheckItemController) + "_" + nameof(CBS_DayCheckItemController.CBSsglbGetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            CBSsglbGetList(PagingModel<SysDataDicDto>
                pagingQueryModel)
        {
            var result = (await _servicedds.GetListAsync()).Where(m => m.Category == pagingQueryModel.Model.Category && !m.IsDeleted).ToList();
            return this.JsonSuccess(result);
        }
        
        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_DayCheckItemController) + "_" + nameof(CBS_DayCheckItemController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_DayCheckItemDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                string SysDepartmentID = await _serviceItem.GetDepartmentId(UserIdentity.Id.ToString());
                if (SysDepartmentID == null || SysDepartmentID == "") { }
                else
                {
                    dto.CreateDept = Guid.Parse(SysDepartmentID);
                }
                dto.CreateFactory = UserIdentity.Factory;
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                string SysDepartmentID = await _serviceItem.GetDepartmentId(UserIdentity.Id.ToString());
                if (SysDepartmentID == null || SysDepartmentID == "") { }
                else
                {
                    dto.UpdateDept = Guid.Parse(SysDepartmentID);
                }
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(CBS_DayCheckItemController) + "_" + nameof(CBS_DayCheckItemController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(CBS_DayCheckItemController) + "_" + nameof(CBS_DayCheckItemController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<CBS_DayCheckItemDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion


    }
}
